using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.ObjectBuilder;
using LibrErian.Model;

namespace LibrErian.Modules.BasicBook
{
    [SmartPart]
    public partial class BookListView : UserControl, IBookListView 
    {
        private BookListViewPresenter presenter;

        [CreateNew]
        public BookListViewPresenter Presenter
        {
            set 
            { 
                presenter = value;
                presenter.View = this;
            }
        }

        public BookListView()
        {
            InitializeComponent();
            InitDataGridView();
        }

        private void InitDataGridView()
        {
            dgvBooks.AutoGenerateColumns = false;

            DataGridViewTextBoxColumn titleColumn = new DataGridViewTextBoxColumn();
            titleColumn.DataPropertyName = "Title";
            titleColumn.HeaderText = "Title";

            DataGridViewTextBoxColumn subTitleColumn = new DataGridViewTextBoxColumn();
            subTitleColumn.DataPropertyName = "Subtitle";
            subTitleColumn.HeaderText = "Subtitle";

            DataGridViewTextBoxColumn isbnColumn = new DataGridViewTextBoxColumn();
            isbnColumn.DataPropertyName = "ISBN";
            isbnColumn.HeaderText = "ISBN";

            DataGridViewTextBoxColumn noOfPagesColumn = new DataGridViewTextBoxColumn();
            noOfPagesColumn.DataPropertyName = "NoOfPages";
            noOfPagesColumn.HeaderText = "NoOfPages";

            DataGridViewTextBoxColumn publicationDateColumn = new DataGridViewTextBoxColumn();
            publicationDateColumn.DataPropertyName = "PublicationDate";
            publicationDateColumn.HeaderText = "Pub. Date";
            publicationDateColumn.DefaultCellStyle.Format = "dd-MM-yyyy";

            DataGridViewTextBoxColumn formatColumn = new DataGridViewTextBoxColumn();
            formatColumn.DataPropertyName = "Format";
            formatColumn.HeaderText = "Format";

            dgvBooks.Columns.AddRange(new DataGridViewColumn[] { titleColumn, subTitleColumn, isbnColumn, noOfPagesColumn, publicationDateColumn, formatColumn });
        }

        public void ShowBooks()
        {
            dgvBooks.DataSource = presenter.GetBooks();
        }

        public void ShowBooks(IList data)
        {
            dgvBooks.DataSource = data;
        }

        private void BookListView_Load(object sender, EventArgs e)
        {
            ShowBooks();
        }

        private void dgvBooks_DoubleClick(object sender, EventArgs e)
        {
            if(dgvBooks.SelectedRows.Count == 1)
            {
                Book book = (Book)dgvBooks.SelectedRows[0].DataBoundItem;
                //presenter.ShowBook(book);
            }
        }

        private void dgvBooks_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvBooks.SelectedRows.Count == 1)
            {
                Book book = (Book)dgvBooks.SelectedRows[0].DataBoundItem;
                presenter.OnSelectionChanged(book);
            }
        }
    }
}
